package com.hlm.demo.service.admininfo;

import com.hlm.demo.entity.AdminInfo;
import com.hlm.demo.entity.AdminRole;
import com.hlm.demo.entity.ex.AdminWithRole;
import com.hlm.demo.mapper.AdminInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminInfoService implements IAdminInfoService {


    private static AdminInfoMapper adminInfoMapper;

    @Autowired
    public AdminInfoService(AdminInfoMapper adminInfoMapper){
        AdminInfoService.adminInfoMapper = adminInfoMapper;
    }

    @Override
    public AdminInfo findOneByAdminId(Integer id) {
        return adminInfoMapper.findOneByAdminId(id);
    }

    /**
     *  查询所有的管理员和管理员所对应的角色
     * @param page 铺页面时的第几页
     * @param count 铺页面时的每页显示的条数
     * @return
     */
    @Override
    public List<AdminWithRole> findAll(Integer page, Integer count, String searhMsg,  Integer moduleId) {

        Map<String,Object> map = new HashMap<>();
        page =( page - 1 ) * count;
        map.put("page",page);
        map.put("count",count);
        if (searhMsg != null && !searhMsg.equals("")){
            map.put("search",searhMsg.trim());
            System.out.println("Service:" + searhMsg);
        }
        if (moduleId != 0){
            map.put("moduleId",moduleId);
        }
        return adminInfoMapper.findAll(map);
    }


    /**
     *  更新一个管理员的信息
     * @param adminInfo Controller层传递过来的管理员对象
     * @param allRole   这个管理员对象对应的所有角色
     * @return
     */
    @Override
    public int updateAdmin(AdminInfo adminInfo, String allRole) {

        int i = 0;

        Integer adminId = adminInfo.getAdminId();
        String name = adminInfo.getAdminName();
        String telephone = adminInfo.getTelephone();
        String email = adminInfo.getEmail();

        AdminInfo byAdminInfo = adminInfoMapper.findOneByAdminId(adminInfo.getAdminId());
        Map<String,Object> map = new HashMap<>();
        map.put("adminId",adminId);
        if (!byAdminInfo.getAdminName().equals(name)){
            map.put("name",name);
        }
        if (!byAdminInfo.getTelephone().equals(telephone)){
            map.put("telephone",telephone);
        }
        if (!byAdminInfo.getEmail().equals(email)){
            map.put("email",email);
        }
        if (map.size() != 1){
            i = adminInfoMapper.updateAdmin(map);
        }

        adminInfoMapper.deleteAdminRole(adminId);

        if (allRole != null){

        addAdminRole(adminId,allRole);
        }

        return i;
    }


    /**
     *  根据id删除某一个管理员
     * @param adminId 管理员的id
     * @return
     */
    @Override
    public int deleteAdmin(Integer adminId) {
        int i = adminInfoMapper.deleteAdmin(adminId);
        adminInfoMapper.deleteAdminRole(adminId);

        return i;
    }

    /**
     *  管理员表有多少条数据
     * @return
     */
    @Override
    public int totalCount(String search,Integer moduleId) {
        Map<String,Object> map = new HashMap<>();
        if (search != null && !search.equals("")){
            map.put("search",search.trim());
            System.out.println("Service:" + search);
        }
        if (moduleId != 0){
            map.put("moduleId",moduleId);
        }
        List<AdminWithRole> roles = adminInfoMapper.totalNum(map);
        return roles.size();
    }


    /**
     *  添加一个管理员
     * @param adminInfo  添加的管理员的对象
     * @param allRole    新添加的管理员对应的角色
     * @return
     */
    @Override

    public int addOneAdmin(AdminInfo adminInfo, String allRole) {

        int i = adminInfoMapper.addOneAdmin(adminInfo);
        Integer adminId = adminInfo.getAdminId();

        if (allRole != null){
            addAdminRole(adminId,allRole);
        }
        return i;
    }

    /**
     *  添加一个角色
     * @param adminRole
     * @return
     */
    @Override
    public int addOneRole(AdminRole adminRole) {
        int i = adminInfoMapper.addOneRole(adminRole);
        return i;
    }





    // 工具方法
    public static void addAdminRole(Integer adminId,String str){
        String[] roles = str.split(",");

        /**
         *  如果把他的权限改为管理员
         */
        if ("100".equals(roles[0])){
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(100);

            adminInfoMapper.addOneRole(adminRole);
        } else {

            for (String s : roles) {
                AdminRole adminRole = new AdminRole();
                adminRole.setAdminId(adminId);
                adminRole.setRoleId(Integer.parseInt(s));

                System.out.println();
                adminInfoMapper.addOneRole(adminRole);
            }
        }

    }
}
